package com.lagou.cl.controller;

import com.lagou.cl.controller.vo.BaseResult;
import com.lagou.cl.controller.vo.ResumeRespVo;
import com.lagou.cl.pojo.Resume;
import com.lagou.cl.service.ResumeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 程  林
 * @date 2020-04-18 18:41
 * @description //
 * @since V1.0.0
 */
@RestController
@RequestMapping("/v1/resume")
public class ResumeController {

	@Autowired
	private ResumeService resumeService;


	@RequestMapping(value = "queryAll", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.GET)
	public ResumeRespVo queryAll() {
		System.out.println("查询数据~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

		ResumeRespVo respVo = new ResumeRespVo();

		List<Resume> resumes = resumeService.queryAll();
		respVo.setData(resumes);

		return respVo;
	}

	@RequestMapping(value = "get/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.GET)
	public ResumeRespVo get(@PathVariable(value = "id") Long id) {

		System.out.println("根据Id获取数据~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

		ResumeRespVo respVo = new ResumeRespVo();

		Resume resume = resumeService.queryById(id);
		ArrayList<Resume> objects = new ArrayList<>();
		objects.add(resume);
		respVo.setData(objects);

		return respVo;
	}

	@RequestMapping(value = "add", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.POST)
	public BaseResult add(@RequestBody Resume resume) {
		System.out.println("新增~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

		BaseResult respVo = null;

		if (StringUtils.isBlank(resume.getName())
				|| StringUtils.isBlank(resume.getAddress())
				|| StringUtils.isBlank(resume.getPhone())) {
			respVo = new BaseResult("9002", "参数为空", false);
			return respVo;
		}

		Resume add = resumeService.add(resume);
		if (add == null) {
			respVo = new BaseResult("9003", "增加失败", false);
			return respVo;
		}

		return new BaseResult();
	}

	@RequestMapping(value = "edit", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.PUT)
	public BaseResult edit(@RequestBody Resume resume) {

		System.out.println("修改~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
		BaseResult respVo = null;

		if (null == resume.getId() || resume.getId() <= 0
				|| StringUtils.isBlank(resume.getName())
				|| StringUtils.isBlank(resume.getAddress())
				|| StringUtils.isBlank(resume.getPhone())) {
			respVo = new BaseResult("9002", "参数为空", false);
			return respVo;
		}

		try {
			Resume add = resumeService.update(resume);
			if (add == null) {
				respVo = new BaseResult("9004", "修改失败", false);
				return respVo;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new BaseResult("9004", "修改失败", false);
		}

		return new BaseResult();
	}

	@RequestMapping(value = "delete/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.DELETE)
	public BaseResult deleteById(@PathVariable(value = "id") Long id) {

		System.out.println("删除~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
		try {
			resumeService.deleteById(id);
		} catch (Exception e) {
			e.printStackTrace();
			return new BaseResult("9001", "删除失败", false);
		}
		return new BaseResult();
	}

}
